package com.syyo.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.product.domain.entity.BrandEntity;
import com.syyo.gulimall.product.domain.req.BrandReq;
import com.syyo.gulimall.product.mapper.BrandMapper;
import com.syyo.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 品牌
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:32:12
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(BrandReq req) {
        BrandEntity brand = new BrandEntity();
        brand.setSort(req.getSort());
        brand.setFirstLetter(req.getFirstLetter());
        brand.setDescript(req.getDescript());
        brand.setShowStatus(req.getShowStatus());
        brand.setName(req.getName());
        brand.setLogo(req.getLogo());
        // 添加创建时间和更新时间
        int insert = brandMapper.insert(brand);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = brandMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(BrandReq req) {
        BrandEntity brand = new BrandEntity();
        // 添加更新时间
        brand.setBrandId(req.getBrandId());
        brand.setName(req.getName());
        brand.setDescript(req.getDescript());
        brand.setShowStatus(req.getShowStatus());
        brand.setFirstLetter(req.getFirstLetter());
        brand.setSort(req.getSort());
        int insert = brandMapper.updateById(brand);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public BrandEntity findOne(Integer id) {
        return brandMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Cacheable(value = "attr", key = "'attrinfo:'+#root.args[0]")
    @Override
    public IPage<BrandEntity> findAll(Integer pageNum, Integer pageSize, BrandReq req) {
        Page<BrandEntity> teacherPage = new Page<BrandEntity>(pageNum, pageSize);
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        return brandMapper.selectPage(teacherPage, wrapper);
    }

    @Override
    public List<BrandEntity> infos(List<Long> ids) {
        return brandMapper.selectBatchIds(ids);
    }

}